﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace hCommand {
    public class TaskNoUtil {
        public static int virtual_Loc = 99999;
        public static int getTaskNo(Sw sw) { 
            String dateline = DateTime.Now.ToString("yyyyMMdd");
            int now = int.Parse(dateline);
            int value = getTaskNoIndex(sw, dateline);
            int head = now % 10000;
            if (value >= 1000) {
                string begin = head + ""+ value;
                return int.Parse(begin);
            } else {
                return  head * 1000 + value;
            }
           
           
        }

        static int getTaskNoIndex(Sw sw, String dateline) {
            string sql = "SELECT * FROM b_param WHERE id=98";
            BParam p = sw.queryUnique<BParam>(sql);
            if (p == null) {
                p = new BParam();
                p.id = 98;
                p.driveType = "taskNo";
                p.keyCode = dateline; 
                p.v = 1;
                p.remark = "任务号序列";
                sw.insert(p);
                return (int)p.v;

            }
            if (p.keyCode == dateline) {
                p.v++;
                string update = "update b_param set v=@v WHERE id=@id";
                int c = sw.exec(update, p);
                return (int)p.v;
            } else {
                p.keyCode = dateline;
                p.v = 1;
                string update = "update b_param set v=@v,key_Code=@keyCode WHERE id=@id";
                int c = sw.exec(update, p);
                return (int)p.v;
            }

        }
    }
}
